package com.hdu;

import lombok.Getter;

import java.util.concurrent.TimeUnit;

public abstract class TimerTask implements Runnable {

    /**
     * 任务延迟多久执行, 单位ms, 3s，则此值为3000
     */
    public final long DELAYMS;

    public TimerTask(long DELAYMS) {
        this.DELAYMS = DELAYMS;
    }

    public TimerTask (long DELAYMS, TimeUnit timeUnit) {
        this.DELAYMS = timeUnit.toMillis(DELAYMS);
    }

    @Getter
    private TimerTaskList.TimerTaskEntry timerTaskEntry = null;

    public synchronized void cancel() {
        if (timerTaskEntry != null) {
            timerTaskEntry.remove();
        }
        timerTaskEntry = null;
    }

    public synchronized void setTimerTaskEntry(TimerTaskList.TimerTaskEntry entry) {
        if (timerTaskEntry != null && timerTaskEntry != entry) {
            timerTaskEntry.remove();
        }
        timerTaskEntry = entry;
    }

}